﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ENG.TeachersDiary.Model;

namespace ENG.TeachersDiary.Classes
{
  class StudentResult
  {
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
    private Model.Student _Student;
    ///<summary>
    /// Sets/gets Student value.
    ///</summary>
    public Model.Student Student
    {
      get
      {
        return (_Student);
      }
      private set
      {
        _Student = value;
      }
    }
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
    private List<MarkGroupResult> _Results = new List<MarkGroupResult>();
    public List<MarkGroupResult> Results
    {
      get
      {
        return (_Results);
      }
      set
      {
        _Results = value;
      }
    }
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
    private SumResult _SumResult;
    ///<summary>
    /// Sets/gets SumResult value.
    ///</summary>
    public SumResult SumResult
    {
      get
      {
        return (_SumResult);
      }
      set
      {
        _SumResult = value;
      }
    }

    private StudentResult(){}

    public StudentResult(Student student)
    {
      this.Student = student;
    }

    public MarkGroupResult GetByMarkGroup (Model.MarkGroup markGroup)
    {
      MarkGroupResult ret = null;

      ret = Results.First(i => i.MarkGroup == markGroup);

        return ret;
    }

    public static StudentResult Create(Model.Student student, Model.Subject subject)
    {

      StudentResult ret = new StudentResult();
      ret.Results = new List<MarkGroupResult>();

      ret.Student = student;

      Program.UpdateProgress("Calculating result for student " + student.NameSurname + " - loading mark groups...");
      var db = Common.DBConn;
      var mgs = db.MarkGroups.Where(i => i.SubjectID == subject.SubjectID);

      foreach (var fItem in mgs)
      {
        if (fItem.IsEvaluated) { 
          Program.UpdateProgress("Calculating result for student " + student.NameSurname + " - calculating results...");
          MarkGroupResult pom = MarkGroupResult.Create(student, fItem);
          ret.Results.Add(pom);
        }
      } // foreach (var fItem in mgs)

      if (Properties.Settings.Default.ContinousSumCalc)
      {
        Program.UpdateProgress("Calculating result for student " + student.NameSurname + " - calculating continous sum...");
        SumResult sr = SumResult.Create(ret.Results);
        ret.SumResult = sr;
      }
      else
        ret.SumResult = null;

      return ret;
    }
  }
}
